import time
from apis import app
from flask.views import MethodView
from flask import request, jsonify
from spiders import zhihu_login
from database import zhihu_cookies_db
from utils.req_utils import get_params


class CookieAPI(MethodView):

    def get(self):
        un = request.args.get('username')
        # 不显示
        ignore_id = {
            '_id': 0
        }
        if not un:
            # 随便取
            result = zhihu_cookies_db.find_one(projection=ignore_id)
        else:
            find_condition = {
                'username': un,
                'enable': 1
            }
            result = zhihu_cookies_db.find_one(find_condition, projection=ignore_id)
        data = {
            'code': 0,
            'data': result if result else {}
        }
        return jsonify(data)

    def post(self):
        un = get_params('username')
        pw = get_params('password')
        if not un or not pw:
            return jsonify({'code': -1, 'msg': 'username或password不可为空'})

        cookies, all_header_info = zhihu_login.login(un, pw)

        time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        data = {
            'username': un,
            'password': pw,
            'cookies': cookies,
            'all_header': all_header_info,
            'date': time_str,
            # 启用状态
            'enable': 1
        }
        zhihu_cookies_db.insert_one(data)
        data.pop('_id')
        data = {
            'code': 0,
            'data': data
        }
        return jsonify(data)

    def delete(self):
        un = request.args.get('username')
        if not un:
            return jsonify({'code': -1, 'msg': 'username不可为空'})

        update_condition = {
            'username': un,
        }
        time_str = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
        # 软删除
        update_data = {
            'enable': 0,
            'date': time_str,
        }
        zhihu_cookies_db.update_one(update_condition, {'$set': update_data})
        # 删除成功
        return jsonify({'code': 0})


app.add_url_rule('/cookie/', view_func=CookieAPI.as_view('cookie'))
